package com.titan.single.controller;

import com.titan.single.constant.KafkaConstant;
import com.titan.single.entity.User;
import com.titan.single.kafka.KafkaProducer;
import com.titan.single.service.TestService;
import com.titan.single.service.UserService;
import com.titan.single.util.JsonUtils;
import com.titan.single.vo.Car;
import com.titan.single.vo.KafkaMessage;
import com.titan.single.vo.Result;
import com.titan.single.vo.UserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;


@RestController
@RequestMapping("/test")
public class TestController {

    private final Logger log = LoggerFactory.getLogger(getClass());


    @Autowired
    private TestService testService;
    @Autowired
    private UserService userService;
    @Autowired
    private KafkaProducer kafkaProducerService;


    @GetMapping(value = "/demo")
    public String demo(@RequestParam String test) throws Exception {
        log.info("开始demo测试");
        testService.test();
        return "测试完毕";
    }


    // 规则引擎测试
    @PostMapping(value = "/liteFlow")
    public String liteFlow(@RequestParam String test) throws Exception {
        log.info("开始liteFlow测试");
        String result = testService.liteFlow(test);
        return result;
    }

    //用户表mybatisPlus-curd测试
    @PostMapping(value = "/getUserList")
    public Result getUserList(@RequestBody UserVo userVo) throws Exception {
        log.info("开始getUserList测试");
        return Result.returnSuccess(userService.selectPageByUserVo(userVo));
    }


    @PostMapping(value = "/getUser")
    public Result getUser() throws Exception {
        log.info("开始用户表getUser测试");
        return Result.returnSuccess(userService.getById(1));
    }

    /**
     * 发送消息
     */
    @PostMapping("/kafka")
    public String sendMessage(@RequestParam Integer type,
                              @RequestParam String message) {
        log.info("开始kafka测试");
        if (Objects.equals(type, 1)) {
        //     发送对象信息
            KafkaMessage kafkaMessage = new KafkaMessage("1", "我是反派", 1L, "zeki");
            String msgJson = JsonUtils.objToString(kafkaMessage);
            kafkaProducerService.sendMessage(KafkaConstant.TOPIC_JSON_MSG, msgJson);
        }else if (Objects.equals(type, 2)) {
            kafkaProducerService.sendMessageWithKey(KafkaConstant.TOPIC_KEY, "777", message);
        }else {
            kafkaProducerService.sendMessage(KafkaConstant.TOPIC_MSG, message);
        }

        return "消息已发送到主题: " + message;
    }

    @PostMapping(value = "/redisCrud")
    public Result redisCrud() throws Exception {
        log.info("开始redisCrud测试");
        testService.redisCrud();
        return Result.returnSuccess("成功");
    }


    @PostMapping(value = "/redisCompress")
    public Result redisCompress() throws Exception {
        log.info("开始redis压缩测试");
        testService.redisCompress();
        return Result.returnSuccess("成功");
    }


    @PostMapping(value = "/completable")
    public Result completable() throws Exception {
        log.info("开始多线程数据聚合测试");
        List<Car> result=  testService.completable();
        return Result.returnSuccess(result);
    }


    public static void main(String[] args) {
        HashMap<String, User> result = new HashMap<>();
        User user = new User();
        user.setId(0);
        user.setUserName("zeki");
        System.out.println(user);
        String s = JsonUtils.objToString(user);
        System.out.println(s);
    }

}
